﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;


using System.IO;
using System.Drawing;
using Utility1;

namespace Thv.admin.Pages
{
    public partial class editsanpham : System.Web.UI.Page
    {
        const string UploadDirectory = "~/trangchu/images/Product/";
   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Bincate();
        }
        void Bincate()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.Categories
                       where k.flag==true
                       select k;
            cbLoai.DataSource = list;
            cbLoai.ValueField = "ID";
            cbLoai.TextField = "CateName";
            cbLoai.DataBind();
            cbLoai.SelectedIndex = 0;
        }
        string SavePostedFile(System.Web.UI.WebControls.FileUpload uploadedFile)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;
           
            Random r = new Random(9999);
            int n = r.Next(999999);
            string ThumbnailFileName = n+FileUpload1.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, 200))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            return ThumbnailFileName;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            Product_tb p = new Product_tb();
            p.iCateID = int.Parse(cbLoai.Value.ToString());
            if (chkGoiy.Checked == true)
                p.goiy = true;
            else
                p.goiy = false;
            if (chktrangchu.Checked == true)
                p.trangchu = true;
            else
                p.trangchu = false;
            p.iProName = txtName.Text;

            if (FileUpload1.HasFile)
            {
                string file = SavePostedFile(FileUpload1);
                p.image = file;
            }
            p.iVersion = 1;
            p.iDatepost = DateTime.Now;
            p.iDateUp = DateTime.Now;
            p.giaSo = double.Parse(txtGia.Value.ToString());
            p.iGiamoi = String.Format("{0:0,0}", double.Parse(txtGia.Value.ToString()));
            p.iDersc = htmMota.Html;
            p.iflag = true;
            string id = "0";
            try
            {
                id = Request.QueryString["id"].ToString();
            }
            catch (Exception) { }
            if (id != "0")
            {
                p = dx.Product_tbs.Single(l => l.ID == int.Parse(id));
            }
            else
            {
                dx.Product_tbs.InsertOnSubmit(p);
            }
            dx.SubmitChanges();
            
        }


    }
}